package com.suineg.pms.commons.wabacus;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suineg.pms.commons.AppConfig;
import com.suineg.pms.entity.base.Building;
import com.suineg.pms.entity.base.Community;
import com.suineg.pms.service.base.BuildingService;
import com.suineg.pms.service.base.CommunityService;
import com.suineg.pms.util.StringUtils;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditActionBean;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.Consts;

/**
 * 楼栋的wabacus拦截器
 *@author Suine
 */
public class BuildingInterceper extends AbsInterceptorDefaultAdapter{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	public int doSavePerAction(ReportRequest rrequest, ReportBean rbean,
			Map<String, String> mRowData, Map<String, String> mParamValues,
			AbsEditActionBean actionbean, AbsEditableReportEditDataBean editbean) {
		// TODO Auto-generated method stub
		
		checkName(rrequest,mRowData);
		return super.doSavePerAction(rrequest, rbean, mRowData, mParamValues,actionbean, editbean);
	}
	
	/**
	 * 检测名的重复性
	 * @param rrequest
	 * @param mRowData
	 */
	public void checkName(ReportRequest rrequest,Map<String, String> mRowData){
		CommunityService communityService = AppConfig.getInstance().getWac().getBean(CommunityService.class);
		try {
			Long communityId = Long.valueOf(mRowData.get("community_id"));
			String id = mRowData.get("id");
			String no = mRowData.get("no");
			String name = mRowData.get("name");
			
			Community community = communityService.get(communityId);
			BuildingService buildingService = AppConfig.getInstance().getWac().getBean(BuildingService.class);
			Building building = null;
			
			if(StringUtils.isEmpty(id)){	//插入
				building = buildingService.findByNo(no);
				
				if(building != null){
					rrequest.getWResponse().getMessageCollector().warn("此楼栋编号已存在!", false, Consts.STATECODE_FAILED);
					return ;
				}
				for(Building b : community.getBuildingList()){
					if(name.equals(b.getName().equals(name))){
						rrequest.getWResponse().getMessageCollector().warn("此楼栋名已存在!", false, Consts.STATECODE_FAILED);
						break;
					}
				}
			}else{							//修改
				building = buildingService.get(Long.valueOf(id));
				if(!no.equals(building.getNo())){			//修改编号 
					Building b = buildingService.findByNo(no);
					if(b != null){
						rrequest.getWResponse().getMessageCollector().warn("此楼栋编号已存在!", false, Consts.STATECODE_FAILED);
						return ;
					}
				}
				if(!building.getName().equals(name)){	//修改名
					for(Building b : community.getBuildingList()){
						if(name.equals(b.getName())){
							rrequest.getWResponse().getMessageCollector().warn("此楼栋名已存在!", false, Consts.STATECODE_FAILED);
							break;
						}
					}
				}
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			rrequest.getWResponse().getMessageCollector().warn("传入的ID不合法！请重新登陆后再试！！", false, Consts.STATECODE_FAILED);
		}catch (Exception e){
			e.printStackTrace();
			rrequest.getWResponse().getMessageCollector().warn("保存失败！", false, Consts.STATECODE_FAILED);
		}
	}

}

